﻿
using System;
using System.Reflection;

namespace CHS.ChengDu.Service
{
    public class CHSChengDu
    {
        static System.Type yh_chs = Type.GetTypeFromProgID("YinHai.CHS.InterfaceSCS");
        static object yh_chs_obj;

        #region yh_CHS_init
        /// <summary>
        /// yh_CHS_init
        /// </summary>
        /// <param name="appcode">状态编码</param>
        /// <param name="appmsg">状态信息</param>
        public  void CHS_Init(ref int appcode, ref string appmsg)
        {
            appcode = -1;
            appmsg = "";

            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }

            object[] args = new object[] { appcode, appmsg };

            ParameterModifier pm = new ParameterModifier(2);
            pm[0] = true;
            pm[1] = true;
            ParameterModifier[] pmd = { pm };
            try
            {
                //调用接口
                yh_chs.InvokeMember("yh_CHS_init",
                BindingFlags.InvokeMethod,
                null,
                yh_chs_obj,
                args,
                pmd,
                System.Globalization.CultureInfo.CurrentCulture,
                null);
                appcode = int.Parse(args[0].ToString());
                appmsg = args[1].ToString();
               // _logInfo.Info("yh_CHS_init appcode:" + appcode + " appmsg:" + appmsg);
            }
            catch (Exception e)
            {
             //   _logInfo.Error(e.Message);
            }

        }
        #endregion

        # region yh_CHS_call 
        /// <summary>
        /// 调用医保安全控件
        /// </summary>
        /// <param name="_infno">交易编号 入口参数</param>
        /// <param name="_input">交易输入 入口参数</param>
        /// <param name="_output">交易输出 出口参数</param>
        /// <returns></returns>

        public  bool CHS_Call(string _infno, string _input, ref string _output)
        {
           // _logInfo.Info("CHS_Call 开始调用 infno:" + _infno + " input:" + _input);
            int code = -1;
            string msg = "";

            //初始化接口
            CHS_Init(ref code, ref msg);

            if (code != 1)
            {
                return false;
            }

            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }
            //交易输入参数
            object[] args = new object[] { _infno, _input, _output };

            ParameterModifier pm = new ParameterModifier(3);
            pm[0] = false;
            pm[1] = false;
            pm[2] = true;
            ParameterModifier[] pmd = { pm };
            try
            {
                yh_chs.InvokeMember("yh_CHS_call",
                               BindingFlags.InvokeMethod,
                               null,
                               yh_chs_obj,
                               args,
                               pmd,
                               System.Globalization.CultureInfo.CurrentCulture,
                               null);
                _output = args[2].ToString();
            //    _logInfo.Info("CHS_Call 调用结束 output:" + _output);
            }
            catch (Exception e)
            {
                //释放函数
                CHS_Destroy();
           //     _logInfo.Error(e);
            }
            //释放函数
            CHS_Destroy();
            return true;
        }

        #endregion

        # region yh_CHS_print 
        /// <summary>
        /// 打印报销单据
        /// </summary>
        /// <param name="_input">交易输入 入口参数</param>
        /// <param name="_output">交易输出 出口参数</param>
        /// <returns></returns>

        public void CHS_Pint(string _input, ref string _output)
        {
            //_logInfo.Info("CHS_Pint 开始调用 input:" + _input);
            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }
            //交易输入参数
            object[] args = new object[] { _input, _output };

            ParameterModifier pm = new ParameterModifier(2);
            pm[0] = false;
            pm[1] = true;
            ParameterModifier[] pmd = { pm };
            try
            {
                yh_chs.InvokeMember("yh_CHS_print",
                BindingFlags.InvokeMethod,
                null,
                yh_chs_obj,
                args,
                pmd,
                System.Globalization.CultureInfo.CurrentCulture,
                null);
                _output = args[1].ToString();
           //     _logInfo.Info("CHS_Pint 调用结束 output:" + _output);
            }
            catch (Exception e)
            {
          //      _logInfo.Error(e.Message);
            }
        }
        #endregion

        #region yh_CHS_destroy         
        /// <summary>
        /// 释放接口
        /// </summary>
        public void CHS_Destroy()
        {
            if (yh_chs_obj == null)
            {
                yh_chs_obj = Activator.CreateInstance(yh_chs);
            }
            //交易输入参数
            object[] args = new object[] { };

            yh_chs.InvokeMember("yh_CHS_destroy",
                BindingFlags.InvokeMethod,
                null,
                yh_chs_obj,
                null,
                null,
                System.Globalization.CultureInfo.CurrentCulture,
                null);
          //  _logInfo.Info("CHS_Destroy 释放接口");
        }
        #endregion

    }
}


